package com.gym.myshop.dao.impl;

import com.gym.myshop.dao.CartDao;
import com.gym.myshop.entity.Cart;
import com.gym.myshop.entity.Product;
import com.gym.myshop.util.DBUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ming
 * 2021/11/6
 * description:
 */
public class CartDaoImpl implements CartDao {
    private QueryRunner qr= new QueryRunner();

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:加入购物车,连表查用户对应其购物车
     * uid用户实体的主键属性 pid商品的唯一主键
     */
    @Override
    public Cart getCartByUidAndPid(int uid, int pid) {
        try {
            //连表查
            Map<String, Object> query = qr.query(DBUtil.getConnection(),
                    "select * from cart c inner join product p on p.pid=c.pid where uid=? and p.pid=?"
                    , new MapHandler(), uid, pid);
            if (query!=null) {
                //1 实例化需要封装数据的对象
                Cart c = new Cart();
                Product p = new Product();
                //2 利用工具类数据的封装
                BeanUtils.populate(c,query);
                BeanUtils.populate(p,query);
                //3 把pro 封装给cart
                c.setPro(p);
                //4 返回数据
                return c;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:修改商品数量
     * uid用户实体的主键属性 pid商品的唯一主键
     */
    @Override
    public int updateCart(int cid, int cnum, double ccount) {

        try {
            return qr.update(DBUtil.getConnection(),
                    "update cart set cnum = ? , ccount = ? where cid=?"
                    ,cnum,ccount,cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:添加商品数量
     * uid用户实体的主键属性 pid商品的唯一主键
     */
    @Override
    public int addCart(Cart cart) {
        try {
            return qr.update(DBUtil.getConnection(),
                    "insert into cart values (default,?,?,?,?)",
                    cart.getUid(),cart.getPid(),cart.getCcount(),cart.getCnum());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:查看购物车数据 封装
     * uid用户实体的主键属性 pid商品的唯一主键
     */
    @Override
    public List<Cart> getCarList(int uid) {
        try {
            List<Map<String, Object>> query = qr.query(DBUtil.getConnection(), "select * from cart c inner join product p on p.pid=c.pid where uid=? ",
                    new MapListHandler(), uid);
            //判断是否有数据
            if (query!=null) {
                List<Cart> cl = new ArrayList<>();
                for (Map<String, Object> stringObjectMap : query) {
                    // 实例化要封装数据的对象
                    Cart c = new Cart();
                    Product pro = new Product();
                    // 数据封装
                    BeanUtils.populate(c,stringObjectMap);
                    BeanUtils.populate(pro,stringObjectMap);
                    c.setPro(pro);
                    // 把封装好数据的对象加入到list中
                    cl.add(c);
                }
                //返回list集合
                return cl;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:清空购物车 uid
     * uid用户实体的主键属性 pid商品的唯一主键
     */
    @Override
    public int clearCart(int uid) {
        try {
            return qr.update(DBUtil.getConnection(),"delete from cart where uid = ?",uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:删除单个商品，通过uid、pid确定
     * uid用户实体的主键属性 pid商品的唯一主键
     */
    @Override
    public int delCart(int uid, Integer pid) {

        try {
            return qr.update(DBUtil.getConnection(),"delete from cart where uid = ? and pid =? ",uid,pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
